const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class PurchaseHandler extends BaseHandler {
    
    get pattern() {
        return "/purchase"
    }

    doGET(request, response) {
        if(!request.session.user.authenticated) {
            response.end(JSON.stringify({
                success: false,
                error: '请登录系统进行购买！'
            }));
        } else {
            var planId = request.session.user.plan_id;
            var query = request.query;
            var item = query.item;
            var itemCount = query.item_count;
            if (!item) {
                response.end(JSON.stringify({
                    success: false,
                    error: '请指定要购买的产品！'
                }));
            } else if(!itemCount) {
                response.end(JSON.stringify({
                    success: false,
                    error: '请指定要购买产品的数量！'
                }));
            } else {
                this.Controller.purchaseProductions(request.session.user.id, planId, item, itemCount, query, (result) => {
                    response.end(JSON.stringify(result));
                });
            }
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = PurchaseHandler;